//
//  WPReadingVideoAndAudioCell.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/1.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReadingVideoAndAudioCell.h"
#import "WPReadingAVCollectionViewCell.h"

@interface WPReadingVideoAndAudioCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

@end
@implementation WPReadingVideoAndAudioCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = [UIColor whiteColor];
        [self collectionView];
    }
    return self;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate= self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        [_collectionView registerClass:[WPReadingAVCollectionViewCell class] forCellWithReuseIdentifier:@"WPReadingAVCollectionViewCell"];
        
        CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
        CGFloat itemHeight = itemWidth*90/112.5;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        
        [self.contentView addSubview:_collectionView];
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.contentView).offset(5);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
    }
    return _collectionView;
}

#pragma mark - collectionView的数据源代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPReadingAVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPReadingAVCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.models[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.audioOrVideoDidClickBlock)
    {
        self.audioOrVideoDidClickBlock(indexPath.item);
    }
}

-(void)setModels:(NSArray *)models
{
    _models = models;
    
    [self.collectionView reloadData];
}

@end
